from django.http import HttpResponse
from common.json import packIntoJsonData
from common.error import commonResponse
from regist.models import Restaurant
from regist.exportmodels import exportreslineinfo
from models import QueueMember

def queryRestaurantQueueInfo(request):
    Result = commonResponse()
    
    if not request.user.is_authenticated(): 
        Result.setcommonError(4); # no login error
        return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")
    else:
        if 'restaurantid' in request.POST:
            restaurantid = request.POST['restaurantid']
            
            res = None 
            try:
                res = Restaurant.objects.get( pk =  restaurantid )
            except  Restaurant.DoesNotExist,AssertionError:
                Result.setcommonError(6); # 6 restaurant record not exist
                return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")        
            
            if res is None :
                Result = commonResponse()
                Result.setcommonError(6); # restaurant record not exist
                return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")
            else:
                Result.setcommonError(0); 
                Result.setuserdata( exportreslineinfo(res) )
                return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")
            
        else:
            Result.setcommonError(-1); # no define error
            return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")


def orderstart(request):   
    Result = commonResponse()
    if request.user.is_authenticated():
        if 'restaurantid' in request.POST:
            restaurantid = request.POST['restaurantid']
            
            res = None 
            try:
                res = Restaurant.objects.get( pk =  restaurantid )
            except  Restaurant.DoesNotExist,AssertionError:
                Result.setcommonError(6); # 6 restaurant record not exist
                return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")        
            if res is not None :
                res.startqueue()
                Result.setcommonError(0) #no error
                return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")
    else:
        
        Result.setcommonError(4) #user hasn't login
        return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")

def orderstop(request):   
    Result = commonResponse()
    if request.user.is_authenticated():
        if 'restaurantid' in request.POST:
            restaurantid = request.POST['restaurantid']
            
            res = None 
            try:
                res = Restaurant.objects.get( pk =  restaurantid )
            except  Restaurant.DoesNotExist,AssertionError:
                Result.setcommonError(6); # 6 restaurant record not exist
                return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")        
            if res is not None :
                res.stopqueue()
                Result.setcommonError(0) #no error
                return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")
    else:
        
        Result.setcommonError(4) #user hasn't login
        return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")
    
def requestapassport(request):    
    Result = commonResponse()
    if request.user.is_authenticated():
        res = None
        try: 
            res = Restaurant.objects.get(pk =  request.POST['restaurantid'])
        except  Restaurant.DoesNotExist,AssertionError:
                Result.setcommonError(6); # 6 restaurant record not exist
                return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")        
             
        if res is None :
            
            Result.setcommonError(6)# restaurant record not exist
            return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")
        else:
            if res.getqueueworkingflag() == False : 
                Result.setcommonError(7) #no error
                return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")
            else:
                sequence = res.jointhequeueline()
                
                new_peopleinqueue = QueueMember(restaurant = res,
                                                user= request.user, 
                                                sequencenumber = sequence
                                                )
                if new_peopleinqueue is not None:                
                    new_peopleinqueue.save()
                    Result.result = 0
                    Result.setuserdata( sequence )
                    return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")
                    
                else:
                    Result.result = -1
                    return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")
                
                #append a record in models
                
    else:
        
        Result.setcommonError(4) #user hasn't login
        return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")

               
def kickoutapassport(request):    
    Result = commonResponse()
    if request.user.is_authenticated():
        res = None
        try: 
            res = Restaurant.objects.get(pk =  request.POST['restaurantid'])
        except  Restaurant.DoesNotExist,AssertionError:
                Result.setcommonError(6); # 6 restaurant record not exist
                return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")        
             
        if res is None :
            
            Result.setcommonError(6)# restaurant record not exist
            return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")
        else:
            if res.administrator == request.user.username:
                pass
            else:
                Result.setcommonError(8)
                return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")
                return 
            if res.getqueueworkingflag() == False : 
                Result.setcommonError(7) #restaurant queue hasn't start yet
                return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")
            else:
                res.callnextone()
                Result.setcommonError(0)
                return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")
                #TODO should be notify everyone in queue
                #FIRST query queue databese and pick suitable people to notify 
    else:
        
        Result.setcommonError(4) #user hasn't login
        return HttpResponse(packIntoJsonData( Result ),mimetype="text/json")

               
  
                              